package com.airsystem.pos.bengkel.service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.airsystem.pos.bengkel.entity.DetailPesanSparepart;
import com.airsystem.pos.bengkel.entity.PesanSparepart;
import com.airsystem.pos.bengkel.interfaces.IServices;
import com.airsystem.pos.bengkel.util.HibernateUtil;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class PesanSparepartServices implements IServices<PesanSparepart> {
	private static final Logger LOG = Logger.getLogger(PesanSparepartServices.class.getSimpleName());

	public void saveOrUpdate(PesanSparepart data) {
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			session.saveOrUpdate(data);
			for (DetailPesanSparepart item : data.getDetail_pesan_sparepart()) {
				session.save(item);
			}

			session.flush();
		} finally {
			session.close();
		}
	}

	public void delete(PesanSparepart data) {
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			session.delete(data);
			session.flush();
		} finally {
			session.close();
		}
	}

	public Iterable<PesanSparepart> select(PesanSparepart data) {
		List<PesanSparepart> list = new LinkedList<PesanSparepart>();
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			Criteria criteria = session.createCriteria(PesanSparepart.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			criteria.createAlias("detail_pesan_sparepart", "detail_pesan_sparepart");
			criteria.addOrder(Order.desc("tanggal"));
			criteria.addOrder(Order.desc("detail_pesan_sparepart.metode"));

			if (data.getTanggal() != null) {
				criteria.add(Restrictions.like("tanggal", data.getTanggal()));
			}

			List<?> results = criteria.list();
			for (Object item : results) {
				PesanSparepart ps = (PesanSparepart) item;
				Hibernate.initialize(ps.getDetail_pesan_sparepart());
				list.add(ps);
			}

			LOG.info("Results size is " + results.size());
		} finally {
			session.close();
		}

		return list;
	}

	public List<DetailPesanSparepart> transactionPrint(Long ID, Date tanggal, String distributor) {
		Session session = HibernateUtil.getSessionFactory().openSession();

		Criteria criteria = session.createCriteria(DetailPesanSparepart.class);
		criteria.add(Restrictions.eq("distributor", distributor));

		Criteria subCriteria = criteria.createCriteria("pesan_sparepart");
		subCriteria.add(Restrictions.eq("id", ID));
		subCriteria.add(Restrictions.eq("tanggal", tanggal));

		List result = criteria.list();
		session.close();

		return result;
	}

	public List<DetailPesanSparepart> print(Date start, Date end, Integer metode) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();

		Criteria criteria = session.createCriteria(DetailPesanSparepart.class);
		Criteria subCriteria = criteria.createCriteria("pesan_sparepart");
		subCriteria.add(Restrictions.between("tanggal", start, end));

		if (metode != 2) {
			criteria.add(Restrictions.eq("metode", metode));
		}

		List result = criteria.list();
		session.close();

		return result;
	}
}